package com.hzjh.base.style.view.loading;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

/**
 * @author hyq
 * @date 2018/7/16
 */
public class SafeImageView extends AppCompatImageView {
    public static final String TAG = SafeImageView.class.getSimpleName();

    public SafeImageView(Context context) {
        super(context, null);
    }

    public SafeImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public SafeImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
        } catch (Exception e) {
            Log.e(TAG, "onDraw catch a exception");
            e.printStackTrace();
        }
    }

    public Bitmap getBitmap() {
        BitmapDrawable drawable = ((BitmapDrawable) getDrawable());
        if (drawable != null) {
            return drawable.getBitmap();
        } else {
            Log.e(TAG, "drawable is null");
            return null;
        }
    }
}
